package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.service.yxweike.YxyResWeikeService;

import io.swagger.annotations.ApiOperation;

@RestController
public class SynLessonChapterItemListWeikeController {

    @Autowired
    private YxyResWeikeService yxyResWeikeService;

    @Autowired
    private YxyResBookContentsRepository yxyResBookContentsRepository;

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @ApiOperation(value = "同步课程-创建课程-添加课时-查询微课")
    @RequestMapping(value = "/api_yw/manage/tec/lessons/synlesson/lstweike", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "id");
        YxyResBookContents bookContents = yxyResBookContentsRepository.findById(reqModel.bookcontent);
        Assert.notNull(bookContents, "目录未找到");
        YxyResBook book = yxyResBookRepository.findById(bookContents.getBookId());
        Assert.notNull(book, "书籍未找到");
        String bookcontent = book.getPhaseName() + "-" + book.getSubjectName() + "-" + book.getVersionName() + "-"
                + book.getSemersterName() + "-" + bookContents.getName();
        Page<ElasticYxyResResource> page = yxyResWeikeService.findByBookContentAndLessonItemNot(reqModel.bookcontent,
                reqModel.lessonId, reqModel.keyword, pageable);
        List<RespModel> respModels = new ArrayList<SynLessonChapterItemListWeikeController.RespModel>();
        for (ElasticYxyResResource elasticYxyResResource : page) {
            RespModel model = new RespModel();
            model.weikeId = elasticYxyResResource.getId();
            model.weikeName = elasticYxyResResource.getTitle();
            model.bookcontent = bookcontent;
            model.duration = elasticYxyResResource.getDuration() + "";
            model.from = "云校";
            // model.vip = elasticYxyResResource.getIsVip() ? "VIP" : "免费";
            respModels.add(model);
        }
        Page<RespModel> respPage = new PageImpl<SynLessonChapterItemListWeikeController.RespModel>(respModels, pageable,
                page.getTotalElements());

        return respPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String lessonId;
        public String bookcontent;

        public String keyword;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(lessonId, null), "请填写课程ID");
            Assert.notNull(StringUtils.defaultIfBlank(bookcontent, null), "词搜索条件下并无微课。");
        }
    }

    protected static class RespModel {
        public String weikeId;
        public String weikeName;
        public String bookcontent;
        public String duration;
        public String from;
        public String vip;
    }
}
